using System;
using System.Drawing;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// The <see cref="IBOSelectorAndEditor"/> for editing the <see cref="DMProperty"/>s of the <see cref="DMClass
    /// </summary>
    public class DMPropertyAndRuleEditorControl : PanelWin, IFormControl, IBOParentChildrenSelector
    {
        private DMClass _dmClass;

        /// <summary>
        /// Gets and Sets the selector control used to select a Business Object Property.
        /// </summary>
        public IBOParentChildrenSelector PropertySelectorControl { get; private set; }

        private readonly DMPropertyEditorControl _propertyEditorControl;

        /// <summary>
        /// Gets and sets the Control used to Edit <see cref="DMProperty"/>s
        /// </summary>
        public IBusinessObjectControl PropertyEditorControl
        {
            get { return _propertyEditorControl; }
        }

        public DMPropertyAndRuleEditorControl(IControlFactory controlFactory)
        {
            PropertySelectorControl = new DMPropertySelectorControl(controlFactory);
            PropertySelectorControl.MinimumSize = new Size(400, 300);
            _propertyEditorControl = new DMPropertyEditorControl(controlFactory);

            this.Height = PropertyEditorControl.Height+100;
            this.Width = 760;
            this.MaximumSize = new Size(this.Width, this.Height);
            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(PropertySelectorControl, BorderLayoutManager.Position.West);
            layoutManager.AddControl(_propertyEditorControl, BorderLayoutManager.Position.Centre, true);
            new BOSelectorAndEditorManager(PropertySelectorControl, PropertyEditorControl);
            new BOSelectorAndEditorManager(PropertySelectorControl, _propertyEditorControl.PropRuleControl);
            GridControl.BusinessObjectSelected += (sender, e) => FireBusinessObjectSelected(e);
            this.Enabled = false;
        }

        private void FireBusinessObjectSelected(BOEventArgs e)
        {
            try
            {
                if (this.BusinessObjectSelected != null)
                {
                    this.BusinessObjectSelected(this, new BOEventArgs(e.BusinessObject));
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }


        /// <summary>
        /// The <see cref="DMClass"/> whose <see cref="DMProperty"/>s are being edited by this control
        /// </summary>
        public DMClass Class
        {
            get { return _dmClass; }
            set
            {
                try
                {
                    _dmClass = value;
                    PropertySelectorControl.BusinessObject = _dmClass;
                    UpdateState();
                }
                catch (Exception ex)
                {
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
            }
        }

        private void UpdateState()
        {
            this.Enabled = _dmClass != null;
        }


        /// <summary>
        /// The <see cref="IBusinessObjectCollection"/> of <see cref="DMProperty"/>s  being managed by this control.
        /// </summary>
        public BusinessObjectCollection<DMProperty> Properties
        {
            get { return (BusinessObjectCollection<DMProperty>) BusinessObjectCollection; }
        }

        /// <summary>
        /// The <see cref="DMProperty"/> currently selected in the Grid (i.e. currently being edited)
        /// </summary>
        public DMProperty Property
        {
            get { return (DMProperty) PropertySelectorControl.SelectedBusinessObject; }
            set { PropertySelectorControl.SelectedBusinessObject = value; }
        }


        public void SetForm(IFormHabanero form)
        {
        }

        public IBusinessObject BusinessObject
        {
            get { return this.Class; }
            set { this.Class = (DMClass) value; }
        }

        public event EventHandler<BOEventArgs> BusinessObjectSelected;

        public void Clear()
        {
            GridControl.Clear();
        }

        public IBusinessObject GetBusinessObjectAtRow(int row)
        {
            return GridControl.GetBusinessObjectAtRow(row);
        }

        public IBusinessObjectCollection BusinessObjectCollection
        {
            get { return GridControl.BusinessObjectCollection; }
            set { GridControl.BusinessObjectCollection = value; }
        }

        public IBusinessObject SelectedBusinessObject
        {
            get { return GridControl.SelectedBusinessObject; }
            set { GridControl.SelectedBusinessObject = value; }
        }

        public int NoOfItems
        {
            get { return GridControl.NoOfItems; }
        }

        /// <summary>
        ///             Gets and sets whether this selector autoselects the first item or not when a new collection is set.
        /// </summary>
        public bool AutoSelectFirstItem
        {
            get { return GridControl.AutoSelectFirstItem; }
            set { GridControl.AutoSelectFirstItem = value; }
        }

        /// <summary>
        /// returns the underlying grid control used to display and select a collection 
        /// of DMProperties
        /// </summary>
        public IGridControl GridControl
        {
            get { return ((DMPropertySelectorControl) PropertySelectorControl).GridControl; }
        }
    }
}